package com.cxyd.modules.wcs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxyd.common.constant.WcsConstants;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.modules.wcs.dao.RouteSetDao;
import com.cxyd.modules.wcs.dto.RouteSetDTO;
import com.cxyd.modules.wcs.entity.RouteSetEntity;
import com.cxyd.modules.wcs.service.RouteSetService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class RouteSetServiceImpl extends CrudServiceImpl<RouteSetDao, RouteSetEntity, RouteSetDTO> implements RouteSetService {
    @Override
    public QueryWrapper<RouteSetEntity> getWrapper(Map<String, Object> params) {
        String dvcNo = (String) params.get("dvcNo");
        String carryNo = (String) params.get("carryNo");
        String taskTyp = (String) params.get("taskTyp");
        String frmPos = (String) params.get("frmPos");
        String toPos = (String) params.get("toPos");
        final QueryWrapper<RouteSetEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(dvcNo), "dvc_no", dvcNo);
        wrapper.like(StringUtils.isNotBlank(carryNo), "carry_no", carryNo);
        wrapper.like(StringUtils.isNotBlank(taskTyp), "task_typ", taskTyp);
        wrapper.like(StringUtils.isNotBlank(frmPos), "frm_pos", frmPos);
        wrapper.like(StringUtils.isNotBlank(toPos), "to_pos", toPos);
        return wrapper;
    }

    @Override
    public RouteSetEntity getRouteSetByPath(String trkType, String from, String to) {
        if (trkType.isEmpty() || from.isEmpty() || to.isEmpty()) {
            return null;
        }
        QueryWrapper<RouteSetEntity> qw = new QueryWrapper<>();
        if (WcsConstants.TRK_TYPE_IN.equals(trkType)) {
            qw.likeRight("frm_pos", "R");
        } else if (WcsConstants.TRK_TYPE_OT.equals(trkType)) {
            qw.likeRight("to_pos", "F");
        } else
            return null;
        return baseDao.selectOne(qw);
    }
}
